package com.jjb.www.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.hjq.shape.view.ShapeTextView;
import com.jjb.www.R;
import com.jjb.www.manager.IntentKey;
import com.jjb.www.ui.activity.BrowserActivity;
import com.jjb.www.utils.ResUtils;


/**
 * 隐私协议弹窗
 */
public class CommonAgreeDialog extends Dialog {

    private static final String TAG = CommonAgreeDialog.class.getSimpleName();
    private ShapeTextView mBtConfirm;
    private TextView mTvCancel;
    private TextView mTvContent;

    private IDialogClickListener mDialogClickListener;

    Activity mActivity;

    private String mContent;
    private String mAppName;

    public CommonAgreeDialog(@NonNull Activity activity, String content, String tagAppName) {
        super(activity, R.style.full_screen_dialog);
        mContent = content;
        mAppName = tagAppName;
        init(activity);
    }

    public CommonAgreeDialog(@NonNull Activity activity, int themeResId) {
        super(activity, themeResId);
        init(activity);
    }

    private void init(Activity activity) {
        mActivity = activity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_splash);
        initWindow();
        mBtConfirm = findViewById(R.id.tv_authorization);
        mTvCancel = findViewById(R.id.tv_cancel);
        mTvContent = findViewById(R.id.tv_hint);
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (mDialogClickListener != null) {
                    mDialogClickListener.onCancelClick();
                }
            }
        });
        mBtConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (mDialogClickListener != null) {
                    mDialogClickListener.onConfirmClick();
                }
            }
        });
        setAgreementContent();
    }


    private void setAgreementContent() {
        if (mContent == null) {
            return;
        }
        try {
            SpannableStringBuilder spannable = new SpannableStringBuilder(mContent);
            String agreeTag = "《服务协议》";
            int userStartIndex = mContent.indexOf(agreeTag);
            int userEndIndex = userStartIndex + agreeTag.length();
            spannable.setSpan(new TextClick(IntentKey.YHXY), userStartIndex, userEndIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            String privacyTag = "《隐私政策》";
            int privacyStartIndex = mContent.indexOf(privacyTag);
            int privacyEndIndex = privacyStartIndex + privacyTag.length();
            spannable.setSpan(new TextClick(IntentKey.YSZC), privacyStartIndex, privacyEndIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            mTvContent.setMovementMethod(LinkMovementMethod.getInstance());
            mTvContent.setText(spannable);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void setOnDialogClickListener(IDialogClickListener listener) {
        mDialogClickListener = listener;
    }


    private void initWindow() {
        WindowManager.LayoutParams dialogParams = getWindow().getAttributes();
        dialogParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        dialogParams.height = WindowManager.LayoutParams.MATCH_PARENT;
    }

    public interface IDialogClickListener {
        void onConfirmClick();

        void onCancelClick();
    }

    private class TextClick extends ClickableSpan {
        private String url;

        public TextClick() {

        }

        public TextClick(String url) {
            this.url = url;
        }

        @Override
        public void onClick(View widget) {
            if (!TextUtils.isEmpty(url)) {
                BrowserActivity.start(mActivity, url);
            }

        }

        @Override
        public void updateDrawState(TextPaint ds) {
            if (ds != null) {
                ds.setColor(ResUtils.getColor(R.color.color_1A6CAF));
            }

        }
    }

}
